Ext.onReady(function(){
	function number(){
		return document.getElementById('droppableH').childNodes[0].childNodes.length;
	}
	function insertText(textarea,text)
	{
	    var tc = Ext.getDom(textarea);
	    var tclen = tc.value.length;
	    tc.focus();
	    if(typeof document.selection != "undefined")
	    {
	        document.selection.createRange().text = text;  
	    }
	    else
	    {
	        tc.setValue(tc.value.substr(0,tc.selectionStart)+text+tc.value.substring(tc.selectionStart,tclen));
	    }
	    //tc.focus();
	}
	var editPanel = new Ext.TabPanel({
		id:'editPanel',
        activeTab: 0,
        height:230,
        width:Ext.getCmp('pivotgridContainer').getWidth(),
		border:false,
		style: 'position:absolute;left:0;bottom:0;z-index:101',
		collapsible: true,
		animCollapse: true,
		border:false,
        defaults:{autoScroll: true},
        items:[new Ext.Panel({
    	id:'zhibiao_message',
    	evObject:null, //正在修改的Li-dom对象
    	title:'指标信息',
		frame:true,
		border:false,
		bodyStyle :'border:none',
		items:[{
            xtype:'panel',
            bodyStyle:'border:none',
            layout:'hbox',
            layoutConfig: {
                padding: 10
            },
            defaults:{
                margins:'0 5 0 0'
            },
            items:[{
				xtype:'button',
				iconCls:'newMeasure',
				text:'新    建',
				handler:function(){
            		Ext.getCmp('zhibiao_message').evObject = null;
            		Ext.getCmp('measureLabel').setValue('');
            		Ext.getCmp('measureExpress').setValue('');
            		this.getEl().dom.style.border = '0px';
				}
            },{
				xtype:'button',
				id:'submit_btn',
				iconCls:'submit',
				text:'提    交',
				handler:function(){
	            	var label = Ext.getCmp('measureLabel').getValue();
	        		var express = Ext.getCmp('measureExpress').getValue();
	        		if(label.trim() == ''){
	        			alert('指标名称不能为空');
	        			Ext.getCmp('measureLabel').focus();
	        			return;
	        		}else if(express.trim() == ''){
	        			alert('指标表达式不能为空');
	        			Ext.getCmp('measureExpress').focus();
	        			return;
	        		}
            		if(Ext.getCmp('zhibiao_message').evObject != null){ 
            			//ev不为空,是修改操作
	            		Ext.getCmp('zhibiao_message').evObject.data.name=Ext.getCmp('measureLabel').getValue();
	            		Ext.getCmp('zhibiao_message').evObject.data.code=Ext.getCmp('measureExpress').getValue();
	            		Ext.getCmp('zhibiao_message').evObject.commit();
            		}else{
            			//是新建操作
            			var record = new Ext.data.Record({name:Ext.getCmp('measureLabel').getValue(),code:Ext.getCmp('measureExpress').getValue()});
            			Ext.getCmp('measures_grid').getStore().add(record);
						Ext.getCmp('zhibiao_message').evObject = record;
            		}
            		this.getEl().dom.style.border = '0px';
				}
            },{
				xtype:'button',
				iconCls:'hide',
				text:'隐    藏',
				handler:function(){
            		Ext.getCmp('editPanel').collapse();
				}
            },{
				xtype:'button',
				text:'函数库',
				iconCls:'fn',
				handler:function(){
	            	var fntools = new Ext.ux.FnTools({submit:function(res){
						if(Ext.getCmp('editPanel').getActiveTab() === Ext.getCmp('zhibiao_message')){
							insertText('measureExpress',res);
						}else if(Ext.getCmp('editPanel').getActiveTab() == Ext.getCmp('allconditions')){
							insertText('conditions_textarea',res);
						}else if(Ext.getCmp('editPanel').getActiveTab() == Ext.getCmp('weidu_message')){
							insertText('weiduExpress',res);
					}
					}});
					fntools.show();
				}
            }]
		},{
			layout:'border',
			bodyStyle:'border:none;',
			height:125,
			items:[{
				region:'center',
	            xtype:'fieldset',
	            title: '当前维度',
	            collapsible: true,
	            autoHeight:true,
	            layout:'form',
	            items:[{
	            	id:'measureLabel',
					xtype:'textfield',
					fieldLabel:'指标名称',
					blankText:'名称不可为空',
					enableKeyEvents:true,
					anchor:'100%',
					listeners:{
	            		keydown:function(){
						Ext.getCmp('submit_btn').getEl().dom.style.border = '1px solid red';
						},
						change:function(){
		            		Ext.getCmp('submit_btn').getEl().dom.style.border = '1px solid red';
		            	}}
				},{
					id:'measureExpress',
					xtype:'textarea',
					fieldLabel:'指标表达式',
					blankText:'表达式不可为空',
					enableKeyEvents:true,
					height:50,
					anchor:'100%',
					listeners:{
					keydown:function(){
						Ext.getCmp('submit_btn').getEl().dom.style.border = '1px solid red';
					},
					change:function(){
						Ext.getCmp('submit_btn').getEl().dom.style.border = '1px solid red';
					}}
				}]
			},{
				region:'center',
	            split:true,
	            region:'east',
	            width:230,
	            bodyStyle:'border:none',
	            html:'<div id="operateBtn"><button/>1</button><button/>2</button><button/>3</button><button/>+</button><button/>(</button><button/>)</button><br>'+
				 	 '<button/>4</button><button/>5</button><button/>6</button><button/>-</button><button/><</button><button/>></button><br>'+
				 	 '<button/>7</button><button/>8</button><button/>9</button><button/>*</button><button/>[]</button><button/>|</button><br>'+
				 	 '<button/>0</button><button/>.</button><button/>=</button><button/>/</button><button/>,</button><button/>\'</button></div>'
			}]
		}]
    })
        /*
        ,new Ext.Panel({
    	id:'weidu_message',
    	evObject:null, //正在修改的Li-dom对象
    	title:'维度信息',
		frame:true,
		border:false,
		bodyStyle :'border:none',
		items:[{
            xtype:'panel',
            bodyStyle:'border:none',
            layout:'hbox',
            layoutConfig: {
                padding: 10
            },
            defaults:{
                margins:'0 5 0 0'
            },
            items:[{
				xtype:'button',
				iconCls:'newMeasure',
				text:'新    建',
				handler:function(){
            		Ext.getCmp('weidu_message').evObject = null;
            		Ext.getCmp('weiduLabel').setValue('');
            		Ext.getCmp('weiduExpress').setValue('');
            		this.getEl().dom.style.border = '0px';
				}
            },{
				xtype:'button',
				id:'weidu_submit_btn',
				iconCls:'submit',
				text:'提    交',
				handler:function(){
	            	var label = Ext.getCmp('weiduLabel').getValue();
	        		var express = Ext.getCmp('weiduExpress').getValue();
	        		if(label.trim() == ''){
	        			alert('维度名称不能为空');
	        			Ext.getCmp('weiduLabel').focus();
	        			return;
	        		}else if(express.trim() == ''){
	        			alert('维度表达式不能为空');
	        			Ext.getCmp('weiduExpress').focus();
	        			return;
	        		}
            		if(Ext.getCmp('weidu_message').evObject != null){ 
            			//ev不为空,是修改操作
	            		Ext.getCmp('weidu_message').evObject.label=Ext.getCmp('weiduLabel').getValue();
	            		Ext.getCmp('weidu_message').evObject.express=Ext.getCmp('weiduExpress').getValue();
	            		Ext.getCmp('weidu_message').evObject.innerText = Ext.getCmp('weiduLabel').getValue();
	            		Ext.getCmp('weidu_message').evObject.customV = 'true';
	            		jQuery(Ext.getCmp('weidu_message').evObject).removeAttr("table");
	            		jQuery(Ext.getCmp('weidu_message').evObject).removeAttr("value_");
	            		Ext.getCmp('weidu_message').evObject.conditions = Ext.getCmp('weidu_condition_Express').getValue();
            		}else{
            			//是新建操作
            			 var li = jQuery("<li oncontextmenu='showmenu(this)'/>");
						 li.attr("id",'column'+number());
						 li.attr("label",Ext.getCmp('weiduLabel').getValue());
						 li.attr("express",Ext.getCmp('weiduExpress').getValue());
						 li.attr("customV",'true');
						 li.attr("conditions",Ext.getCmp('weidu_condition_Express').getValue());
						 li.addClass('v');
						 jQuery("#droppableV").find('ul').append(li);
						 li.get(0).innerText = Ext.getCmp('weiduLabel').getValue();
						 Ext.getCmp('weidu_message').evObject = li.get(0);
            		}
            		this.getEl().dom.style.border = '0px';
				}
            },{
				xtype:'button',
				iconCls:'rollback',
				text:'撤    消',
				handler:function(){
            		
				}
            },{
				xtype:'button',
				iconCls:'hide',
				text:'隐    藏',
				handler:function(){
            		Ext.getCmp('editPanel').collapse();
				}
            },{
				xtype:'button',
				text:'函数库',
				handler:function(){
					var fntools = new Ext.ux.FnTools({submit:function(res){
						if(Ext.getCmp('editPanel').getActiveTab() === Ext.getCmp('zhibiao_message')){
							insertText('measureExpress',res);
						}else if(Ext.getCmp('editPanel').getActiveTab() == Ext.getCmp('allconditions')){
							insertText('conditions_textarea',res);
						}else if(Ext.getCmp('editPanel').getActiveTab() == Ext.getCmp('weidu_message')){
							insertText('weiduExpress',res);
					}
					}});
					fntools.show();
				}
            }]
		}
		,{
			layout:'border',
			bodyStyle:'border:none;',
			height:125,
			items:[{
				region:'center',
	            xtype:'fieldset',
	            title: '当前维度',
	            collapsible: true,
	            autoHeight:true,
	            layout:'form',
	            items:[{
	            	id:'weiduLabel',
					xtype:'textfield',
					fieldLabel:'维度名称',
					blankText:'名称不可为空',
					enableKeyEvents:true,
					width:200,
					listeners:{
	            		keydown:function(){
							Ext.getCmp('weidu_submit_btn').getEl().dom.style.border = '1px solid red';
						},
						change:function(){
		            		Ext.getCmp('weidu_submit_btn').getEl().dom.style.border = '1px solid red';
		            	}}
				},{
					id:'weiduExpress',
					xtype:'textarea',
					fieldLabel:'维度表达式',
					blankText:'表达式不可为空',
					enableKeyEvents:true,
					height:30,
					width:400,
					listeners:{afterrender:function(){
							//邦定拖拽到维度表达式
							new Ext.dd.DropTarget(Ext.getCmp('weiduExpress').getEl().dom, {
								ddGroup: 'DDGroup',
								copy: false,
								notifyDrop: function(ddSource, e, data){
								node = ddSource.dragData.node;
								var a=Ext.getCmp('weiduExpress');
								if(node.attributes.d == 'h'){
									//a.setValue(a.getValue()+'$'+node.attributes.table+'.'+node.attributes.column);
								}else if(node.attributes.d == 'v'){
									a.setValue(a.getValue()+'$'+node.attributes.table+'.'+node.attributes.val);
								}
								Ext.getCmp('weidu_submit_btn').getEl().dom.style.border = '1px solid red';
								return(true);
							}
							});
					},
					keydown:function(){
						Ext.getCmp('weidu_submit_btn').getEl().dom.style.border = '1px solid red';
					},
					change:function(){
						Ext.getCmp('weidu_submit_btn').getEl().dom.style.border = '1px solid red';
					}}
				}]
			},{
				region:'center',
	            split:true,
	            region:'east',
	            width:230,
	            bodyStyle:'border:none',
	            html:'<div id="weidu_operateBtn"><button/>1</button><button/>2</button><button/>3</button><button/>+</button><button/>(</button><button/>)</button><br>'+
				 	 '<button/>4</button><button/>5</button><button/>6</button><button/>-</button><button/><</button><button/>></button><br>'+
				 	 '<button/>7</button><button/>8</button><button/>9</button><button/>*</button><button/>[]</button><button/>|</button><br>'+
				 	 '<button/>0</button><button/>.</button><button/>=</button><button/>/</button><button/>,</button><button/>\'</button></div>',
			 	listeners:{afterrender:function(){
						//给条件的小键盘绑定样式和事件
						jQuery('#weidu_operateBtn > button').css("padding",'2px').css('width','30px');
						jQuery('#weidu_operateBtn > button').click(function(){
							insertText('weiduExpress',jQuery(this).text());
							Ext.getCmp('weidu_submit_btn').getEl().dom.style.border = '1px solid red';
						});
				}}
			}]
		}
		]
    })
    */
    ,new Ext.Panel({
    	id:'allconditions',
    	title:'查询条件',
		frame:true,
		border:false,
		bodyStyle :'border:none',
		items:[{
            xtype:'panel',
            bodyStyle:'border:none',
            layout:'hbox',
            layoutConfig: {
                padding: 10
            },
            defaults:{
                margins:'0 5 0 0'
            },
            items:[{
				xtype:'button',
				iconCls:'hide',
				text:'隐    藏',
				handler:function(){
            		Ext.getCmp('editPanel').collapse();
				}
            },{
				xtype:'button',
				text:'函数库',
				iconCls:'fn',
				handler:function(){
	            	var fntools = new Ext.ux.FnTools({submit:function(res){
						if(Ext.getCmp('editPanel').getActiveTab() === Ext.getCmp('zhibiao_message')){
							insertText('measureExpress',res);
						}else if(Ext.getCmp('editPanel').getActiveTab() == Ext.getCmp('allconditions')){
							insertText('conditions_textarea',res);
						}else if(Ext.getCmp('editPanel').getActiveTab() == Ext.getCmp('weidu_message')){
							insertText('weiduExpress',res);
					}
					}});
					fntools.show();
				}
            }]
		},{
			layout:'border',
			bodyStyle:'border:none;',
			height:125,
			items:[{
	            xtype:'fieldset',
	            title: '当前条件',
	            collapsible: true,
	            autoHeight:true,
	            layout:'form',
	            region:'center',
	            items:[{
					id:'conditions_textarea',
					xtype:'textarea',
					fieldLabel:'条件表达式',
					blankText:'表达式不可为空',
					enableKeyEvents:true,
					height:60,
					anchor:'100%',
					listeners:{afterrender:function(){
						//邦定拖拽到查询条件
						new Ext.dd.DropTarget(Ext.getCmp('conditions_textarea').getEl().dom, {
							ddGroup: 'measureGridDD',
							copy: false,
							notifyDrop: function(ddSource, e, data){
							node = ddSource.dragData.node;
							var a=Ext.getCmp('conditions_textarea');
							if(node.attributes.d == 'h'){
								a.setValue(a.getValue()+'$'+node.attributes.table+'.'+node.attributes.column);
							}else if(node.attributes.d == 'v'){
								if(node.attributes.val.indexOf("[tree]") == -1){
									a.setValue(a.getValue()+' and $'+node.attributes.mainTable+'.'+node.attributes.mainColumn + " = '" + node.attributes.val + "'");
								}else{
									a.setValue(a.getValue()+' and $'+node.attributes.mainTable+'.'+node.attributes.mainColumn + " like '" + node.attributes.pid + "%'");
								}
							}
							return(true);
						}
						});
						//回显全局条件
						Ext.getCmp('conditions_textarea').setValue(jQuery("#globleConditions").text());
					}}
				}]
			},{
				region:'center',
	            split:true,
	            region:'east',
	            width:230,
	            bodyStyle:'border:none',
	            html:'<div id="conditionBtn"><button/>1</button><button/>2</button><button/>3</button><button/>+</button><button/>(</button><button/>)</button><br>'+
				 	 '<button/>4</button><button/>5</button><button/>6</button><button/>-</button><button/><</button><button/>></button><br>'+
				 	 '<button/>7</button><button/>8</button><button/>9</button><button/>*</button><button/>[]</button><button/>|</button><br>'+
				 	 '<button/>0</button><button/>.</button><button/>=</button><button/>/</button><button/>,</button><button/>\'</button></div>',
				listeners:{afterrender:function(){
					//给条件的小键盘绑定样式和事件
					jQuery('#conditionBtn > button').css("padding",'2px').css('width','30px');
					jQuery('#conditionBtn > button').click(function(){
						insertText('conditions_textarea',jQuery(this).text());
					});
				}}
			}]}
	       ],
	       listeners:{afterrender:function(){
				Ext.getCmp('editPanel').doLayout();
			}}
    })],
    listeners:{expand:function(){
        	Ext.getCmp('editPanel').doLayout();
    }}
	});

	editPanel.render(Ext.getCmp('pivotgridContainer').getEl());
	editPanel.doLayout();
	//editPanel.collapse(false);
	
	//给指标的小键盘绑定样式和事件
	jQuery('#operateBtn > button').css("padding",'2px').css('width','30px');
	jQuery('#operateBtn > button').click(function(){
		insertText('measureExpress',jQuery(this).text());
		Ext.getCmp('submit_btn').getEl().dom.style.border = '1px solid red';
	});
	
	//邦定拖拽
	new Ext.dd.DropTarget(Ext.getCmp('measureExpress').getEl().dom, {
		ddGroup: 'measureGridDD',
		copy: false,
		notifyDrop: function(ddSource, e, data){
		node = ddSource.dragData.node;
		var a=Ext.getCmp('measureExpress');
		if(node.attributes.d == 'h'){
			a.setValue(a.getValue()+'sum('+node.getOwnerTree().xmlLabel+'$'+node.attributes.table+'.'+node.attributes.column+')');
		}else if(node.attributes.d == 'v'){
			//if(node.attributes.val.indexOf("[tree]") == -1){
				//a.setValue(a.getValue()+'$'+node.attributes.table+'.'+node.attributes.column + " = '" + node.attributes.val + "'");
			//}else{
				//a.setValue(a.getValue()+'$'+node.attributes.tableName+'.'+node.attributes.column + " like '" + node.attributes.pid + "%'");
			//}
		}
		Ext.getCmp('submit_btn').getEl().dom.style.border = '1px solid red';
		return(true);
	}
	});
	
//	zhibiao_message.resizer = new Ext.Resizable(zhibiao_message.el, {
//        minHeight:50,
//        handles: "n",
//        pinned: true,
//        transparent:true,
//        resizeElement : function(){
//            var box = this.proxy.getBox();
//            this.proxy.hide();
//            zhibiao_message.setHeight(box.height);
//            return box;
//        }
//    });
})